var mongoose=require('mongoose');
var Post=mongoose.model('Post',{
    title:String,
    authors:[String],
    comment:{
        good:Number,
        general:Number,
        comments:[
            {
                nick:String,
                content:String,
                like:Number
            }
        ]
    }

});
var db=mongoose.connect('mongodb://localhost/MongoDB_QuanWei_2');

var title='post12-2';
Post.update({title:title},{
    comment:{
        good:95,
        general:3,
        comments:[
            {
                nick:'Tom',
                content:'c1',
                like:10
            },
            {
                nick:'Tom',
                content:'c2',
                like:20
            }
        ]
    }
},{upsert:true},(err,r)=>{
    console.log('Update:',err,r);

    Post.update({title:title,'comment.comments.nick':'Tom'},{
        $set:{
            'comment.comments.$.nick':'Lili'
        }
    },(e,r)=>{
        console.log('定位操作：',e,r);

        Post.find({title:title},(e,r)=>{
            console.log('Find:\n',e);
            console.log(JSON.stringify(r,null,2));
            process.exit(1);
        });
    });

});


/*
 * $ 定位符
 *
 * 1.先指定具体更新条件
 * 2.使用$符号代替符合更新条件的第一个元素
 * 3.只会更新符合条件的第一个元素
 * */





